//go:build windows

package logger

import (
	"container/list"
	"log"
	"sync"
	"syscall"
	"time"
)

type color int

var (
	kernel32    *syscall.LazyDLL  = syscall.NewLazyDLL(`kernel32.dll`)
	proc        *syscall.LazyProc = kernel32.NewProc(`SetConsoleTextAttribute`)
	CloseHandle *syscall.LazyProc = kernel32.NewProc(`CloseHandle`)
)
var (
	//black     color = 0
	red       color = 4
	green     color = 2
	yellow    color = 6
	blue      color = 1
	violet    color = 3
	darkGreen color = 10
	white     color = 15
)

type colorLogInfo struct {
	c color
	v string
}

var logCatch *list.List
var mutex sync.Mutex

func init() {
	logCatch = list.New()
	go logColorShow()
}

func logColorPrint(c color, value string) {
	mutex.Lock()
	defer mutex.Unlock()
	logCatch.PushBack(colorLogInfo{c: c, v: value})
}

func logColorShow() {
	for {
		if logCatch.Len() > 0 {
			mutex.Lock()
			front := logCatch.Front().Value.(colorLogInfo)
			logCatch.Remove(logCatch.Front())

			handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(front.c))
			log.Printf("%v", front.v)
			_, _, _ = CloseHandle.Call(handle)
			mutex.Unlock()
		} else {
			time.Sleep(1 * time.Second)
		}
	}
}
